package com.jiamao.goplay.services;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.jiamao.goplay.ui.main.MainActivity;
import com.netease.nimlib.sdk.mixpush.MiPushMessageReceiver;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;

import org.json.JSONObject;

import java.util.List;

import me.goldze.mvvmhabit.utils.KLog;

import static android.content.Context.ACTIVITY_SERVICE;

public class MiJGPushMessageReceiver extends MiPushMessageReceiver {



    public void onReceivePassThroughMessage(Context context, MiPushMessage message) {
        KLog.d("jpush",message.toString());
    }

    public void onNotificationMessageClicked(Context context, MiPushMessage message) {


        try {
            JSONObject jb = new JSONObject(message.getContent()).getJSONObject("m_content").getJSONObject("n_extras");
              String data =  jb.toString();
            KLog.d("jpush","isRun "+isRun(context,"com.jiamao.goplay"));
           //KLog.d("jpush",message.toString()+" "+isRunning(context,"com.jiamao.goplay"));
            startApp(context,data);


        }catch (Exception e){
            e.printStackTrace();
        }


    }

    public void onNotificationMessageArrived(Context context, MiPushMessage message) {
        KLog.d("jpush",message.toString());
    }

    public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) {
        KLog.d("jpush",message.toString());
    }

    public void onCommandResult(Context context, MiPushCommandMessage message) {
        KLog.d("jpush",message.toString());
    }



    public void startApp(Context context,String message){


        if(isRun(context,"com.jiamao.goplay")){
            try{
                //打开自定义的Activity
                Intent i = new Intent(context, MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("data",message);
                i.putExtras(bundle);
                //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            }catch (Throwable throwable){

            }
        }else {
            Intent launchIntent = context.getPackageManager().
                    getLaunchIntentForPackage("com.jiamao.goplay");
            launchIntent.setFlags(
                    Intent.FLAG_ACTIVITY_NEW_TASK);
            launchIntent.putExtra("data", message);
            context.startActivity(launchIntent);

        }


    }




    /**
     * 判断应用是否在运行
     * @param context
     * @return
     */
    public boolean isRun(Context context,String name){
        ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
        boolean isAppRunning = false;
      //  String MY_PKG_NAME = "com.ad";
        //100表示取的最大的任务数，info.topActivity表示当前正在运行的Activity，info.baseActivity表系统后台有此进程在运行
        for (ActivityManager.RunningTaskInfo info : list) {
            if (info.topActivity.getPackageName().equals(name) || info.baseActivity.getPackageName().equals(name)) {
                isAppRunning = true;
                Log.i("ActivityService isRun()",info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName());
                break;
            }
        }
        Log.i("ActivityService isRun()", "com.ad 程序  ...isAppRunning......"+isAppRunning);
        return isAppRunning;
    }


}
